package com.bank.backend.controller;

import com.bank.backend.result.Result;
import com.bank.backend.model.dto.LoginDTO;
import com.bank.backend.model.vo.UserVO;
import com.bank.backend.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

/**
 * 认证接口
 */
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param loginDTO 登录请求
     * @param request 请求
     * @return 登录用户信息（包含token）
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserVO> login(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
        if (loginDTO == null) {
            return Result.error("请求参数为空");
        }
        String loginId = loginDTO.getLoginId();
        String password = loginDTO.getPassword();
        UserVO userVO = userService.userLogin(loginId, password, request);
        return Result.success(userVO);
    }

    /**
     * 用户登出
     *
     * @param request 请求
     * @return 登出结果
     */
    @PostMapping("/logout")
    public Result<Boolean> logout(HttpServletRequest request) {
        boolean result = userService.userLogout(request);
        return Result.success(result);
    }
}